package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepsitory;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepsitory travelCommentRepsitory;

    @Autowired
    private MongoTemplate template;




    //容易让mongodb产生误会,后续自动生成的id不会存进去
    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        //维护关联的评论
        String refId = travelComment.getRefComment().getId();
        //判断有无Id
        if (StringUtils.hasLength(refId)){
            //有id则为第二层评论
            TravelComment comment = this.get(refId);
            //设置进评论中
            travelComment.setRefComment(comment);
            //设置为评论的评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }else {
            //为控则设置为普通评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }
        travelCommentRepsitory.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepsitory.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepsitory.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        return travelCommentRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> list() {
        return travelCommentRepsitory.findAll();
    }

    //查询游记评论页面
    @Override
    public List<TravelComment> querytravelCommentBytravelId(TravelCommentQuery qo) {
       List<TravelComment> list = travelCommentRepsitory.findByTravelId(qo.getTravelId());
        return list;
    }
}

